Lesson 4: if/then/else and loop structures
By
Moyack
These kind of structures are the most important in any
programming language. In JASS, these structures are quite versatile
and useful.
Let's see the first structure: IF/THEN/ELSE
To
work with it, we can write it in this way.
if/then/else
structure
if <condition 1 function or variable> then |
Now you understand why I said
versatile :)
As you can see, the conditions must be a function
which has to return a boolean variable type (true/false) or a boolean
variable. Other interesting feature with the if/then/else structure
in JASS, is that it can be used as a multiselection structure too,
something very useful in many cases.
In order to ensure the
understanding of this structure, This is a small example showing the
IF/THEN/ELSE in action
IF/THEN/ELSE example
function Set_Damage_To_Unit takes integer level returns real |
Here we are using a local variable which will have a value
according to the input argument in the function, and then that
variable will be returned by the function.
|
Important note |
|
If any IF or ELSEIF clauses apply, then the ELSE clause will be avoided. This example function has a bug by purpose, in order to show an IF/THEN/ELSE characteristic. if level = 1, then the first clause will be executed (set dam = 0). Then the second condition will be executed and will be true too (elseif level == 1), then it will execute the second clause (set dam = 50). With that we can see that the IFTHENELSE structure can be used to make data filtering, for instance. |
|
Another Important Note |
|
The '=' character is used when setting a value, the '==' operator is used when comparing values. In other words, don't do: if x = 5 then... because Wc3 can't read your mind as to what you want it to really do. This line is saying to the computer "if (set x = 5) then..." and I don't think that makes much sense, how about you? |
Now let's see the LOOP structure.
Loops in programming are
used to make a set of instructions to be executed several times,
loops can be classified by finite and infinite. The finite loops are
which have a defined a fixed number of repetitive executions,
controlled by an exit control. The Infinite loops in the other hand,
will execute the script contained in it indefinitely.
The loop
structure is as following:
|
loop |
This structure is quite flexible as
you can see, because you can put the exitwhen condition in ANY
place inside the loop. Let's see an example.
Loop Example
function Heal_Unit_Group takes group g returns nothing |
The usage of FirstOfGroup and group functions inside a loop is very common in AOE spells, with them we can do several things to a unit group.
|
Some tips with loops |
|
Sometimes, we want to use the loops as a way to do a mass
effect spell. If you want to add special effects inside a loop,
there is a big chance the code will lag, in order to avoid that,
you should use a TriggerSleepAction() function, which will
give to the WC3 engine a small break to process other tasks. |
|
Exercise |
|
|
Create a function which displays all the numbers which can divide perfectly a given number. The function should be in that way:
For exaple, if we make DivideNumbers(12) we should see on the game screen something like this: -Also, it would be helpful to tell you about the ModoloInteger function. This function returns the remainder of a division. So ModoloInteger(10, 3) would return 1. |
Good luck